//
//  NSDictionary+MutableDeepCopy.m
//  TestCFile
//
//  Created by jun.wang on 14/11/16.
//  Copyright (c) 2014年 jun.wang. All rights reserved.
//

#import "NSDictionary+MutableDeepCopy.h"

@implementation NSDictionary (MutableDeepCopy)

- (NSMutableDictionary *)mutableDeepCopy {
    NSMutableDictionary *returnDict = [[NSMutableDictionary alloc]
                                       initWithCapacity:[self count]];
    NSArray *keys = [self allKeys];
    for (id key in keys) {
        id oneValue = [self valueForKey:key];
        id oneCopy = nil;
        //适配数字问题
        if ([oneValue isKindOfClass:[NSNumber class]]) {
            oneValue = [NSString stringWithFormat:@"%@",oneValue];
        }
        if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]){
             oneCopy = [oneValue mutableDeepCopy];
        }
        else if ([oneValue respondsToSelector:@selector(mutableCopy)]){
            if(oneValue == nil || oneValue == NULL ||[oneValue isKindOfClass:[NSNull class]])
            {
                oneCopy = [oneValue copy];
            }else{
              oneCopy = [oneValue mutableCopy];
            }
        }
        if (oneCopy == nil)
            oneCopy = [oneValue copy];
        [returnDict setValue:oneCopy forKey:key];
    }
    return returnDict;
}
@end



@implementation NSArray (MutableDeepCopy)
- (NSMutableArray*)mutableDeepCopy {
    NSUInteger count = [self count];
    id cArray[count];
    for (NSUInteger i = 0; i < count; ++i) {
        id obj = [self objectAtIndex:i];
        
        if(obj == nil || obj == NULL ||[obj isKindOfClass:[NSNull class]])
        {
//            NSLog(@"xxx");
        }
//        NSLog(@"--------%@",obj);
        if ([obj respondsToSelector:@selector(mutableDeepCopy)]) {
            cArray[i] = [obj mutableDeepCopy];
        }
        else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)]) {
            cArray[i] = [obj mutableCopy];
        }
        else if ([obj respondsToSelector:@selector(copyWithZone:)]) {
            cArray[i] = [obj copy];
        }
        else {
            return nil;
        }
    }
    NSMutableArray *ret = [NSMutableArray arrayWithObjects:cArray count:count];
    return ret;
}
@end

